<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程类型" prop="courseType">
        <el-select v-model="queryParams.courseType" placeholder="请选择课程类型" clearable size="small">
          <el-option v-for="dict in courseTypes" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程标题" prop="courseTitle">
        <el-input v-model="queryParams.courseTitle" placeholder="请输入课程标题" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="课程标签" prop="courseLabels">
        <el-select filterable v-model="queryParams.courseLabels" placeholder="请选择课程标签" clearable size="small" multiple>
          <el-option v-for="dict in courseLabels" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程教师" prop="teacherIds">
        <el-select filterable v-model="queryParams.teacherIds" placeholder="请选择课程教师" clearable size="small" multiple>
          <el-option v-for="dict in teacherList" :key="dict.id" :label="dict.teacherName" :value="dict.id"></el-option>
        </el-select>
      </el-form-item>            
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleType"
          v-hasPermi="['system:dict:edit']"
        >课程类型</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleLabel"
          v-hasPermi="['system:dict:edit']"
        >课程标签</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:teacher:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:teacher:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:teacher:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:teacher:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange" ref="table" @sort-change="sortChange">      
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="排序号" align="center" prop="orderNum" sortable="custom" width="90"/>
      <el-table-column label="课程类型" align="center" prop="courseType" width="100" :formatter="getCourseType"/>
      <el-table-column label="课程标题" align="center" prop="courseTitle"/>
      <el-table-column label="课程内容" align="center" prop="courseContent">
        <template slot-scope="scope">
          <p style="width:100%;height:200px;overflow:hidden;" v-html='scope.row.courseContent'></p>
        </template>
      </el-table-column>
      <el-table-column label="课程标签" align="center" prop="labelName" width="150"/>
      <el-table-column label="课程教师" align="center" prop="teacherId" width="150" :formatter="getTeacher"/>
      <el-table-column label="浏览量" align="center" prop="browseNum" width="100" sortable="custom"/>
      <el-table-column label="视频封面" align="center" prop="videoCover" width="170">
        <template slot-scope="scope">
          <imageUpload v-model="scope.row.videoCover" :table="scope.row.id+':'+'course'+':'+'pic'"/>          
        </template>
      </el-table-column>
      <el-table-column label="视频链接地址" align="center" prop="videoUrl" width="160">
        <template slot-scope="scope">
          <FileUploadV v-model="scope.row.videoUrl" v-if="scope.row.videoUrl===null||scope.row.videoUrl===''" :table="scope.row.id+':'+'course'+':'+'vid'"/>
          <video controls="controls" width="100%" v-if="scope.row.videoUrl!=null&&scope.row.videoUrl!=''">
            <source type="video/mp4" :src="scope.row.videoUrl"/>
          </video>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:teacher:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:teacher:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"            
            @click="creatQrCode(scope.row)"
            v-hasPermi="['business:teacher:edit']"
          >生成二维码</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【优选好课】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="课程类型" prop="courseType">
              <el-select v-model="form.courseType" placeholder="请选择课程类型" clearable style="width:100%;">
                <el-option v-for="dict in courseTypes" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程教师" prop="teacherId">
              <el-select v-model="form.teacherId" placeholder="请选择课程教师" clearable style="width:100%;">
                <el-option v-for="dict in teacherList" :key="dict.id" :label="dict.teacherName" :value="dict.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="课程标题" prop="courseTitle">
              <el-input v-model="form.courseTitle" placeholder="请输入课程标题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="课程内容" prop="courseContent">
              <editor v-model="form.courseContent" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="课程标签" prop="courseLabel">
              <el-select v-model="form.courseLabel" placeholder="请选择课程标签" clearable style="width:100%;" multiple>
                <el-option v-for="dict in courseLabels" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="排序号" prop="orderNum">
              <el-input v-model="form.orderNum" placeholder="请输入排序号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="浏览量" prop="browseNum">
              <el-input v-model="form.browseNum" placeholder="请输入浏览量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="视频地址" prop="videoUrl">
              <el-input v-model="form.videoUrl" placeholder="请输入视频链接地址" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <qrcodeD ref="qrcodeD" :dialogVisible="openQrCode" @cancelQr="cancelQr"></qrcodeD>
  </div>
</template>

<script>
import { listCourse, getCourse, delCourse, addCourse, updateCourse, exportCourse } from "@/api/business/course";
import { listTeacherAll } from "@/api/business/teacher";
import Editor from '@/components/Editor';
import ImageUpload from '@/components/ImageUpload';
import FileUploadV from '@/components/FileUploadV';
import VideoPlay from '@/components/VideoPlay';
import qrcodeD from "@/components/qrcodeD";
export default {
  name: "Course",
  components: {
    Editor,
    ImageUpload,
    FileUploadV,
    VideoPlay,
    qrcodeD
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【优选好课】表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openQrCode: false,
      courseTypes: [],
      courseLabels: [],
      teacherList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseType: null,
        courseTitle: null,
        courseContent: null,
        courseLabels: null,
        teacherIds: null,
        browseNum: null,
        videoCover: null,
        videoUrl: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseType: [
          { required: true, message: "课程类型不能为空", trigger: "change" }
        ],
        courseTitle: [
          { required: true, message: "课程标题不能为空", trigger: "blur" }
        ],
        courseContent: [
          { required: true, message: "课程内容不能为空", trigger: "blur" }
        ],
        teacherId: [
          { required: true, message: "课程教师不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts('course_type').then(response => {
      this.courseTypes = response.data;
    });
    this.getDicts('course_label').then(response => {
      this.courseLabels = response.data;
    });    
    listTeacherAll().then(response => {
        this.teacherList = response.rows;
    });
  },
  methods: {
    getCourseType(row) {
      for (let i in this.courseTypes) {
        if (this.courseTypes[i].dictValue === row.courseType+'') {
          return this.courseTypes[i].dictLabel;
        }
      }
    },
    getTeacher(row) {
      for (let i in this.teacherList) {
        if (this.teacherList[i].id === row.teacherId) {
          return this.teacherList[i].teacherName;
        }
      }
    },
    sortChange(column) {
      this.loading = true;
      if (column.prop === 'orderNum') {
        this.queryParams.order = 'order_num';
      } else if (column.prop === 'browseNum') {
        this.queryParams.order = 'browse_num';
      }
      if (column.order === 'ascending') {
        this.queryParams.orderType = 'asc';
      } else if (column.order === 'descending') {
        this.queryParams.orderType = 'desc';
      }
      this.courseList = [];
      listCourse(this.queryParams).then(response => {
        response.rows.forEach(item => {
          this.courseList.push(item);
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询【优选好课】列表 */
    getList() {
      this.loading = true;
      this.courseList = [];      
      listCourse(this.queryParams).then(response => {
        response.rows.forEach(item => {
          this.courseList.push(item);
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        courseType: null,
        courseTitle: null,
        courseContent: null,
        courseLabel: null,
        teacherId: null,
        browseNum: null,
        videoCover: null,
        videoUrl: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleType() {
      this.$router.push("/system/dict-data/index/11");
    },
    handleLabel() {
      this.$router.push("/system/dict-data/index/12");
    },
    creatQrCode(row) {
      let host = '';
      if (row.appId=='wxae0f0d0aef1a8645') {
        host = 'train.shudingkj.com';
      }else if (row.appId=='wx49c781286d16deea') {
        host = 'sell.shudingkj.com';
      }else if (row.appId=='wxff9cb9b0b37dfef8') {
        host = 'jkhmzw.shudingkj.com';
      }
      let curPath = 'http://'+host+'/pages/course/detail/index?id='+row.id;
      this.$refs.qrcodeD.paths = curPath;
      this.openQrCode = true;
    },
    cancelQr() {
      this.openQrCode = false;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优选好课";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const courseId = row.id || this.ids
      getCourse(courseId).then(response => {
        this.form = response.data;
        if (this.form.courseLabel) {
          let labelAry = this.form.courseLabel.split(',');
          let labelIdAry = new Array();
          labelAry.forEach(item => {
            labelIdAry.push(item+'');
          })
          this.form.courseLabel = labelIdAry;
        }
        this.open = true;
        this.form.courseType = this.form.courseType+'';
        this.title = "修改优选好课 【若替换视频文件，删除链接内容即可】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseLabel&&this.form.courseLabel.length>0) {
            this.form.courseLabel = this.form.courseLabel.join(',');
          }else {
            this.form.courseLabel = null;
          }
          if (this.form.id != null) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const courseIds = row.id || this.ids;
      this.$confirm('是否确认删除优选好课编号为"' + courseIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCourse(courseIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有优选好课数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCourse(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
